import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

// 学生表操作类
class StudentTableOperator {
    private Connection connection;

    public StudentTableOperator(Connection mysqlConnection) {
        this.connection = mysqlConnection;
    }

    public void createTable() throws SQLException {
        Statement statement = connection.createStatement();
        String sql = "CREATE TABLE IF NOT EXISTS student (" +
                "学号 INT PRIMARY KEY," +
                "姓名 VARCHAR(255)," +
                "性别 VARCHAR(10)," +
                "出生日期 DATE," +
                "家庭地址 VARCHAR(255)," +
                "移动电话 VARCHAR(20)" +
                ")";
        statement.executeUpdate(sql);
        statement.close();
    }

    public void insertStudent(int id, String name, String gender, String birthDate, String address, String phone) throws SQLException {
        String sql = "INSERT INTO student (学号, 姓名, 性别, 出生日期, 家庭地址, 移动电话) VALUES (?,?,?,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, id);
        statement.setString(2, name);
        statement.setString(3, gender);
        statement.setString(4, birthDate);
        statement.setString(5, address);
        statement.setString(6, phone);
        statement.executeUpdate();
        statement.close();
    }

    public void deleteStudent(int id) throws SQLException {
        String sql = "DELETE FROM student WHERE 学号 =?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, id);
        statement.executeUpdate();
        statement.close();
    }

    public void updateStudent(int id, String name, String gender, String birthDate, String address, String phone) throws SQLException {
        String sql = "UPDATE student SET 姓名 =?, 性别 =?, 出生日期 =?, 家庭地址 =?, 移动电话 =? WHERE 学号 =?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, name);
        statement.setString(2, gender);
        statement.setString(3, birthDate);
        statement.setString(4, address);
        statement.setString(5, phone);
        statement.setInt(6, id);
        statement.executeUpdate();
        statement.close();
    }

    public void selectStudent(int id) throws SQLException {
        String sql = "SELECT * FROM student WHERE 学号 =?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, id);
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            int studentId = resultSet.getInt("学号");
            String studentName = resultSet.getString("姓名");
            String studentGender = resultSet.getString("性别");
            String studentBirthDate = resultSet.getString("出生日期");
            String studentAddress = resultSet.getString("家庭地址");
            String studentPhone = resultSet.getString("移动电话");
            System.out.println("学号: " + studentId + ", 姓名: " + studentName + ", 性别: " + studentGender +
                    ", 出生日期: " + studentBirthDate + ", 家庭地址: " + studentAddress + ", 移动电话: " + studentPhone);
        }
        resultSet.close();
        statement.close();
    }
}